#!/usr/bin/env python
# -*- coding:utf-8 -*- 

from flask_restful import Resource, reqparse
from flask import jsonify

from controller.equipmentController import EquipmentController
from utils import commons
from utils.response_code import RET, error_map_EN
import json


class EquipmentResource(Resource):

    # get
    @classmethod
    def get(cls, equipment_id=None):
        if equipment_id:
            kwargs = {
                'equipment_id': equipment_id
            }

            res = EquipmentController.get(**kwargs)
            print(res)
            if res['code'] == RET.OK:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])
            else:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])

        parser = reqparse.RequestParser()
        parser.add_argument('auto_id', location='args', required=False, help='auto_id参数类型不正确或缺失')
        parser.add_argument('equipment_id', location='args', required=False, help='equipment_id参数类型不正确或缺失')
        parser.add_argument('equipment_name', location='args', required=False, help='equipment_name参数类型不正确或缺失')
        parser.add_argument('picture', location='args', required=False, help='picture参数类型不正确或缺失')
        parser.add_argument('count', location='args', required=False, help='count参数类型不正确或缺失')
        parser.add_argument('description', location='args', required=False, help='description参数类型不正确或缺失')
        parser.add_argument('is_delete', location='args', required=False, help='is_delete参数类型不正确或缺失')
        parser.add_argument('update_time', location='args', required=False, help='update_time参数类型不正确或缺失')
        parser.add_argument('create_time', location='args', required=False, help='create_time参数类型不正确或缺失')
        
        parser.add_argument('Page', location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', location='args', required=False, help='Size参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = EquipmentController.get(**kwargs)
        for item in res['data']:
            item['picture'] = item['picture'].decode()
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'], totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['data']) 

    
    # delete
    @classmethod
    def delete(cls, equipment_id=None):
        if equipment_id:
            kwargs = {
                'equipment_id': equipment_id
            }

        else:
            return jsonify(code=RET.PARAMERR, message=error_map_EN[RET.PARAMERR], data='id不能为空')

        res = EquipmentController.delete(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    
    # put
    @classmethod
    def put(cls, equipment_id):
        if not equipment_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('equipment_name', location='form', required=False, help='equipment_name参数类型不正确或缺失')
        parser.add_argument('picture', location='form', required=False, help='picture参数类型不正确或缺失')
        parser.add_argument('count', location='form', required=False, help='count参数类型不正确或缺失')
        parser.add_argument('description', location='form', required=False, help='description参数类型不正确或缺失')
        parser.add_argument('update_time', location='form', required=False, help='update_time参数类型不正确或缺失')
        parser.add_argument('create_time', location='form', required=False, help='create_time参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['equipment_id'] = equipment_id

        res = EquipmentController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    
    # add
    @classmethod
    def post(cls):
        '''
        EquipmentList: Pass in values in JSON format to batch add
        eg.[{k1:v1,k2:v2,...},...]
        '''
        parser = reqparse.RequestParser()
        parser.add_argument('EquipmentList', type=str, location='form', required=False, help='EquipmentList参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        if kwargs.get('EquipmentList'):
            kwargs['EquipmentList'] = json.loads(kwargs['EquipmentList'])
            for data in kwargs['EquipmentList']:
                for key in ['is_delete']:
                    data.pop(key, None)
            res = EquipmentController.add_list(**kwargs)

        else:
            parser.add_argument('equipment_name', location='form', required=False, help='equipment_name参数类型不正确或缺失')
            parser.add_argument('picture', location='form', required=False, help='picture参数类型不正确或缺失')
            parser.add_argument('count', location='form', required=False, help='count参数类型不正确或缺失')
            parser.add_argument('description', location='form', required=False, help='description参数类型不正确或缺失')
            parser.add_argument('update_time', location='form', required=False, help='update_time参数类型不正确或缺失')
            parser.add_argument('create_time', location='form', required=False, help='create_time参数类型不正确或缺失')
            
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)

            res = EquipmentController.add(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])
